from P1_python_基础.c3_设计模式.n1_单例.单例 import *
from loguru import logger


# 配置工具类
class ConfigUtils:

    # 读取YML配置内容
    @staticmethod
    def get_app_config(keys='', default=None):
        # 读取YAML文件
        data = Config().get_config()
        if keys:
            config_data = ConfigUtils.get_config_value(data, keys)
            if config_data:
                return config_data
            else:
                return default
        else:
            return data

    @staticmethod
    def get_config_value(data, keys):
        keys = keys.split('.')
        for key in keys:
            try:
                int(key)
            except Exception:
                pass
            else:
                key = int(key)
            if not data:
                logger.info(f'根据{keys}获取yml配置为空！')
                return None
            if isinstance(data, list):
                data = data[key]
            elif isinstance(data, dict):
                data = data.get(key)
            else:
                return data
        return data


if __name__ == '__main__':
    # 获取yml对应的配置内容
    print(ConfigUtils.get_app_config("test.yml_value"))
